{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using SymPy\n",
    "using LinearAlgebra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 庞加莱群Casimir不变算符的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  Levi-Civita记号\n",
    "ε= [ Sym((length(Set([i,j,k]))==3)*       #  存在重复指标，取0\n",
    "    (-1)^(sum([i,j,k][σ]>[i,j,k][ρ]   \n",
    "            for σ in 1:3,ρ in  1:3  if σ<ρ) % 2)) # 计算逆序数。偶数取1，奇数取-1\n",
    "    for  i in 1:3, j in 1:3, k in 1:3 ] ;\n",
    "\n",
    "# 单位矩阵\n",
    "δ = sympy.diag(1,1,1);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 庞加莱群李代数的结构常数\n",
    "C=[Sym(0) for i in 1:10 , j in 1:10 , k in 1:10];\n",
    "C[5:7,5:7,5:7]=IM*ε;\n",
    "C[8:10,8:10,5:7]=-IM*ε;\n",
    "C[2:4,5:7,2:4]=IM*ε;   C[5:7,2:4,2:4]=-IM*ε;\n",
    "C[1,8:10,2:4]=IM*δ;    C[8:10,1,2:4]=-IM*δ;\n",
    "C[2:4,8:10,1]=IM*δ;    C[8:10,2:4,1]=-IM*δ;\n",
    "C[5:7,8:10,8:10]=IM*ε; C[8:10,5:7,8:10]=-IM*ε;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 庞加莱群的十个参数\n",
    "@vars p0 p1 p2 p3 j1 j2 j3 k1 k2 k3 real=true\n",
    "a=[p0,p1,p2,p3,j1,j2,j3,k1,k2,k3];\n",
    "\n",
    "# 庞加莱群的十个无穷小生成元\n",
    "@vars P0 P1 P2 P3 J1 J2 J3 K1 K2 K3 real=true\n",
    "E=[P0,P1,P2,P3,J1,J2,J3,K1,K2,K3];"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算独立Casimir不变算符的个数\n",
    "\n",
    "$$\n",
    "l=n-\\mathrm{rank}\\ \\color{red}{C^\\sigma_{\\ \\ \\mu\\upsilon} a_\\sigma},\\quad \\color{red}{标注的部分是n\\times n 矩阵}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\\left[ \\begin{array}{rrrrrrrrrr}0&0&0&0&0&0&0&i P_{1}&i P_{2}&i P_{3}\\\\0&0&0&0&0&i P_{3}&- i P_{2}&i P_{0}&0&0\\\\0&0&0&0&- i P_{3}&0&i P_{1}&0&i P_{0}&0\\\\0&0&0&0&i P_{2}&- i P_{1}&0&0&0&i P_{0}\\\\0&0&- i P_{3}&i P_{2}&0&i J_{3}&- i J_{2}&0&i K_{3}&- i K_{2}\\\\0&i P_{3}&0&- i P_{1}&- i J_{3}&0&i J_{1}&- i K_{3}&0&i K_{1}\\\\0&- i P_{2}&i P_{1}&0&i J_{2}&- i J_{1}&0&i K_{2}&- i K_{1}&0\\\\- i P_{1}&- i P_{0}&0&0&0&- i K_{3}&i K_{2}&0&- i J_{3}&i J_{2}\\\\- i P_{2}&0&- i P_{0}&0&i K_{3}&0&- i K_{1}&i J_{3}&0&- i J_{1}\\\\- i P_{3}&0&0&- i P_{0}&- i K_{2}&i K_{1}&0&- i J_{2}&i J_{1}&0\\end{array}\\right]\\]"
      ],
      "text/plain": [
       "10×10 Array{Sym,2}:\n",
       "     0      0      0      0      0      0      0   I*P1   I*P2   I*P3\n",
       "     0      0      0      0      0   I*P3  -I*P2   I*P0      0      0\n",
       "     0      0      0      0  -I*P3      0   I*P1      0   I*P0      0\n",
       "     0      0      0      0   I*P2  -I*P1      0      0      0   I*P0\n",
       "     0      0  -I*P3   I*P2      0   I*J3  -I*J2      0   I*K3  -I*K2\n",
       "     0   I*P3      0  -I*P1  -I*J3      0   I*J1  -I*K3      0   I*K1\n",
       "     0  -I*P2   I*P1      0   I*J2  -I*J1      0   I*K2  -I*K1      0\n",
       " -I*P1  -I*P0      0      0      0  -I*K3   I*K2      0  -I*J3   I*J2\n",
       " -I*P2      0  -I*P0      0   I*K3      0  -I*K1   I*J3      0  -I*J1\n",
       " -I*P3      0      0  -I*P0  -I*K2   I*K1      0  -I*J2   I*J1      0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[sum(C[i,j,k]*E[k] for k in 1:10) for i in 1:10,j in 1:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "CA = [sum(C[i,j,k]*a[k] for k in 1:10) for i in 1:10,j in 1:10];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CA.rank()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\\left[ \\begin{array}{rrrrrrrrrr}0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&6&0&0&0&0&0\\\\0&0&0&0&0&6&0&0&0&0\\\\0&0&0&0&0&0&6&0&0&0\\\\0&0&0&0&0&0&0&2&0&0\\\\0&0&0&0&0&0&0&0&2&0\\\\0&0&0&0&0&0&0&0&0&2\\end{array}\\right]\\]"
      ],
      "text/plain": [
       "10×10 Array{Sym,2}:\n",
       " 0  0  0  0  0  0  0  0  0  0\n",
       " 0  0  0  0  0  0  0  0  0  0\n",
       " 0  0  0  0  0  0  0  0  0  0\n",
       " 0  0  0  0  0  0  0  0  0  0\n",
       " 0  0  0  0  6  0  0  0  0  0\n",
       " 0  0  0  0  0  6  0  0  0  0\n",
       " 0  0  0  0  0  0  6  0  0  0\n",
       " 0  0  0  0  0  0  0  2  0  0\n",
       " 0  0  0  0  0  0  0  0  2  0\n",
       " 0  0  0  0  0  0  0  0  0  2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Killing型\n",
    "κ=[sum(C[μ,σ,ρ]*C[υ,ρ,σ] for σ in 1:10, ρ in 1:10) for μ in 1:10 ,υ in 1:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}0\\end{equation*}"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "κ.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\\left[ \\begin{array}{rrrrrrrrrr}0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&0&0&0&0&0&0\\\\0&0&0&0&\\frac{1}{6}&0&0&0&0&0\\\\0&0&0&0&0&\\frac{1}{6}&0&0&0&0\\\\0&0&0&0&0&0&\\frac{1}{6}&0&0&0\\\\0&0&0&0&0&0&0&\\frac{1}{2}&0&0\\\\0&0&0&0&0&0&0&0&\\frac{1}{2}&0\\\\0&0&0&0&0&0&0&0&0&\\frac{1}{2}\\end{array}\\right]\\]"
      ],
      "text/plain": [
       "10×10 Array{Sym,2}:\n",
       " 0  0  0  0    0    0    0    0    0    0\n",
       " 0  0  0  0    0    0    0    0    0    0\n",
       " 0  0  0  0    0    0    0    0    0    0\n",
       " 0  0  0  0    0    0    0    0    0    0\n",
       " 0  0  0  0  1/6    0    0    0    0    0\n",
       " 0  0  0  0    0  1/6    0    0    0    0\n",
       " 0  0  0  0    0    0  1/6    0    0    0\n",
       " 0  0  0  0    0    0    0  1/2    0    0\n",
       " 0  0  0  0    0    0    0    0  1/2    0\n",
       " 0  0  0  0    0    0    0    0    0  1/2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iκ = pinv(κ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 庞加莱群的Casimir不变算符\n",
    "\n",
    "下面的`Julia`代码有一个约定（其实以前的笔记也默认用到这个约定）：\n",
    "\n",
    "- 1）一个矢量（比如：$P$）默认是逆变矢量；协变矢量则对应记作：$Pi$；\n",
    "- 2）逆变矢量的分量在`Julia`代码中以下标体现（因为上标会被解释成“幂次”）；而协变矢量的分量则通过度规用逆变矢量的分量表出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  四维evi-Civita记号\n",
    "ε4= [ Sym((length(Set([i,j,k,l]))==4)*       #  存在重复指标，取0\n",
    "    (-1)^(sum([i,j,k,l][σ]>[i,j,k,l][ρ]   \n",
    "            for σ in 1:4,ρ in  1:4  if σ<ρ) % 2)) # 计算逆序数。偶数取1，奇数取-1\n",
    "    for  i in 1:4, j in 1:4, k in 1:4,l in 1:4 ] ;\n",
    "\n",
    "# 闵氏度规\n",
    "η = sympy.diag(-1,1,1,1);\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[\\left[ \\begin{array}{rrrr}0&- K_{1}&- K_{2}&- K_{3}\\\\K_{1}&0&J_{3}&- J_{2}\\\\K_{2}&- J_{3}&0&J_{1}\\\\K_{3}&J_{2}&- J_{1}&0\\end{array}\\right]\\]"
      ],
      "text/plain": [
       "4×4 Array{Sym,2}:\n",
       "  0  -K1  -K2  -K3\n",
       " K1    0   J3  -J2\n",
       " K2  -J3    0   J1\n",
       " K3   J2  -J1    0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "                            \n",
    "# J:  两上标洛伦兹生成元矩阵\n",
    "# Ji: 两下标洛伦兹生成元矩阵\n",
    "J=[Sym(0) for i in 1:4 , j in 1:4];\n",
    "J[2:4,2:4]=[sum(ε[k,i,j]*E[4+k] for k in 1:3) for i in 1:3,j in 1:3];\n",
    "J[1,2:4]=-E[8:10];\n",
    "J[2:4,1]=E[8:10];\n",
    "Ji=[sum(J[μ,υ]*η[μ,ρ]*η[υ,σ] for μ in 1:4,υ in 1:4)  for ρ in 1:4,σ in 1:4];\n",
    "J\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}P_{0}\\\\P_{1}\\\\P_{2}\\\\P_{3}\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "4-element Array{Sym,1}:\n",
       " P0\n",
       " P1\n",
       " P2\n",
       " P3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# P:  逆变平移生成元（动量）\n",
    "# Pi: 协变平移生成元（动量）\n",
    "P=E[1:4];\n",
    "Pi = [sum(η[υ,σ]*P[σ] for σ in 1:4) for υ in 1:4];\n",
    "P\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}- J_{1} P_{1} - J_{2} P_{2} - J_{3} P_{3}\\\\- J_{1} P_{0} + K_{2} P_{3} - K_{3} P_{2}\\\\- J_{2} P_{0} - K_{1} P_{3} + K_{3} P_{1}\\\\- J_{3} P_{0} + K_{1} P_{2} - K_{2} P_{1}\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "4-element Array{Sym,1}:\n",
       " -J1*P1 - J2*P2 - J3*P3\n",
       " -J1*P0 + K2*P3 - K3*P2\n",
       " -J2*P0 - K1*P3 + K3*P1\n",
       " -J3*P0 + K1*P2 - K2*P1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# W:  上标Pauli–Lubanski矢量\n",
    "# Wi:  上标Pauli–Lubanski矢量\n",
    "W=Sym(-1//2)*[sum(ε4[μ,υ,ρ,σ]*Ji[υ,ρ]*Pi[σ] \n",
    "        for υ in 1:4,ρ in 1:4,σ in 1:4) \n",
    "    for μ in 1:4];\n",
    "Wi=Sym(-1//2)*[sum(ε4[μ,υ,ρ,σ]*J[υ,ρ]*P[σ] \n",
    "        for υ in 1:4,ρ in 1:4,σ in 1:4) \n",
    "    for μ in 1:4];\n",
    "W"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}- P_{0}^{2} + P_{1}^{2} + P_{2}^{2} + P_{3}^{2}\\\\- \\left(J_{1} P_{0} - K_{2} P_{3} + K_{3} P_{2}\\right)^{2} + \\left(J_{1} P_{1} + J_{2} P_{2} + J_{3} P_{3}\\right)^{2} - \\left(J_{2} P_{0} + K_{1} P_{3} - K_{3} P_{1}\\right)^{2} - \\left(J_{3} P_{0} - K_{1} P_{2} + K_{2} P_{1}\\right)^{2}\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "2-element Array{Sym,1}:\n",
       "                                                                                     -P0^2 + P1^2 + P2^2 + P3^2\n",
       " -(J1*P0 - K2*P3 + K3*P2)^2 + (J1*P1 + J2*P2 + J3*P3)^2 - (J2*P0 + K1*P3 - K3*P1)^2 - (J3*P0 - K1*P2 + K2*P1)^2"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 两个Casimir不变算符\n",
    "PP = sum(P[μ]*Pi[μ] for μ in 1:4);\n",
    "WW = sum(W[μ]*Wi[μ] for μ in 1:4);\n",
    "[PP,WW] .|> simplify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}- m^{2}\\\\- m^{2} \\left(J_{1}^{2} + J_{2}^{2} + J_{3}^{2}\\right)\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "2-element Array{Sym,1}:\n",
       "                      -m^2\n",
       " -m^2*(J1^2 + J2^2 + J3^2)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@vars m ω  real=true  positive=true\n",
    "\n",
    "# m > 0\n",
    "[PP,WW] .|> (expr->expr(P0=>m,P1=>0,P2=>0,P3=>0)) .|> simplify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\[ \\left[ \\begin{array}{r}0\\\\- ω^{2} \\left(\\left(J_{1} - K_{2}\\right)^{2} + \\left(J_{2} + K_{1}\\right)^{2}\\right)\\end{array} \\right] \\]"
      ],
      "text/plain": [
       "2-element Array{Sym,1}:\n",
       "                                0\n",
       " -ω^2*((J1 - K2)^2 + (J2 + K1)^2)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# m = 0\n",
    "[PP,WW] .|> (expr->expr(P0=>ω,P1=>0,P2=>0,P3=>ω)) .|> simplify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}0\\end{equation*}"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PW = sum(Pi[μ]*W[μ] for μ in 1:4) .|> simplify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}- \\frac{J_{1} P_{1} + J_{2} P_{2} + J_{3} P_{3}}{P_{0}}\\end{equation*}"
      ],
      "text/plain": [
       "-(J₁⋅P₁ + J₂⋅P₂ + J₃⋅P₃) \n",
       "─────────────────────────\n",
       "            P₀           "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "W[1] ./ P[1]   |> simplify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}- J_{3}\\end{equation*}"
      ],
      "text/plain": [
       "-J₃"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "W[1] ./ P[1] |> (expr->expr(P0=>ω,P1=>0,P2=>0,P3=>ω))  |> simplify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    " #  |> (expr->print(string(\"\\$\\$\",sympy.latex(expr),\"\\$\\$\"))) "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.4.1",
   "language": "julia",
   "name": "julia-1.4"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
